package com.rjjs.Xiaozhi.Config;


import com.rjjs.Xiaozhi.store.MongoChatMemoryStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.TextDocumentParser;
import dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
public class XiaozhiAgentConfig {

    Document document = FileSystemDocumentLoader.loadDocument("src/main/resources/RAG/my-test.docx");
    Document document1 =FileSystemDocumentLoader.loadDocument("src/main/resources/RAG/my-test1.docx");
    //Document document2 =FileSystemDocumentLoader.loadDocument("src/main/resources/RAG/eye.txt");
    Document document2=FileSystemDocumentLoader.loadDocument("src/main/resources/RAG/幼儿推拉教室门事件(1).txt");




    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private  MongoChatMemoryStore mongoChatMemoryStore;


    @Bean
   public ChatMemoryProvider chatMemoryProvider(){
       return userId->
               MessageWindowChatMemory.builder()
                       .id(userId)
                       .maxMessages(20)
                       .chatMemoryStore(mongoChatMemoryStore)
                       .build();
   }

    @Autowired
    private EmbeddingModel embeddingModel;

 @Bean
 public ContentRetriever contentRetriever(){

//     InMemoryEmbeddingStore<TextSegment> embeddingStore =new InMemoryEmbeddingStore<>();//新建嵌入存储实例
//     List<Document> documents = Arrays.asList(document,document1,document3);//接收文本写入
//     EmbeddingStoreIngestor.ingest(documents,embeddingStore);//执行文档摄入的主逻辑
//
//     return  EmbeddingStoreContentRetriever.builder()//检索策略设计 使用构建者模式配置
//             .embeddingStore(embeddingStore)//指定嵌入存储实例
//             .embeddingModel(embeddingModel)//指定向量存储数据库
//             .maxResults(1)//设置只返回最匹配的一条信息，解决大模型幻觉
//             .minScore(0.8)//向量阈值设为0.8
//             .build();//完成实例构建

     InMemoryEmbeddingStore<TextSegment> embeddingStore =new InMemoryEmbeddingStore<>();
     List<Document> documents =Arrays.asList(document,document1,document2);
     EmbeddingStoreIngestor.ingest(documents,embeddingStore);

     return EmbeddingStoreContentRetriever.builder()
             .embeddingStore(embeddingStore)
             .embeddingModel(embeddingModel)
             .minScore(0.8)
             .maxResults(1)
             .build();
 }


}
